package efuliBusiness;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import easyDAO.SqlDispatch;
import impl.TestServerBusinessImpl;
import models.HttpResponse;
import models.MyConfig;
import models.ReturnJson;
import models.SqlReturnModel;
import tools.Http;

import javax.servlet.http.HttpServletRequest;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.Objects;

/**
 * Created by yangxianming on 2019/5/7.
 * 核销
 */
public class Verification implements TestServerBusinessImpl {
    @Override
    public String doIt(HttpServletRequest request, MyConfig myConfig) {
        ReturnJson returnJson = new ReturnJson();
        //读取请求json
        InputStreamReader inputStreamReader;
        BufferedReader bufferedReader;
        String requestInfo = "";

        try {
            inputStreamReader = new InputStreamReader(request.getInputStream(), "UTF-8");
            bufferedReader = new BufferedReader(inputStreamReader);
            String line;
            while ((line = bufferedReader.readLine()) != null)
                requestInfo = requestInfo + line;
        }catch (Exception e){
            //请求流如果出现异常的时候
            returnJson.setApiStatus(0);
            returnJson.setErrorMsg(e.getMessage());
            return returnJson.toString();
        }

        HttpResponse httpResponse = new HttpResponse();
        JSONObject apiJson = JSON.parseObject(requestInfo);
        JSONObject verificationData = apiJson.getJSONObject("verificationData");
        String mark = "";
        String verifiesOrCancel = apiJson.getString("verifiesOrCancel");
        if (Objects.equals("feima", apiJson.getString("whichVerifies"))){
            Http http = new Http();
            JSONObject header = new JSONObject();
            header.put("Content-Type", "application/json");
            String url = "";
            if (Objects.equals("verifies", verifiesOrCancel)){
                url = "http://115.159.65.101:8001/api/VoucherVerification";
                mark = "核销";
            }
            if (Objects.equals("cancel", verifiesOrCancel)){
                //撤销需要取出之前的订单号 SELECT orderid FROM verification WHERE voucherNumber = ""
                url = "http://115.159.65.101:8001/api/VoucherCancel";
                mark = "核销撤销";
                String sql = "SELECT transId FROM verification WHERE voucherNumber = \"" + verificationData.getString("voucherNumber") + "\" ORDER BY createtime DESC LIMIT 1";
                SqlDispatch sqlDispatch = new SqlDispatch(myConfig);
                SqlReturnModel srm = sqlDispatch.selectWord(sql);
                verificationData.put("transId", srm.getSelectReturnData().get(0).get(0));
            }
            httpResponse = http.sendRequest("post", header, url, verificationData.toJSONString());
        }
        if (Objects.equals("yibai", apiJson.getString("whichVerifies"))){
            //稍等
        }

        // INSERT INTO `verification` (`voucherNumber`, `orderid`) VALUES ('23423423', 'wewerw')
        // 如果核销成功就记录数据库
        JSONObject feimaReturn = JSONObject.parseObject(httpResponse.getResponseResult());
        if (Objects.equals("1", feimaReturn.getString("resultCode"))){
            if (Objects.equals("verifies", verifiesOrCancel)){
                //成功核销的就入库
                JSONObject d = new JSONObject();
                d.put("voucherNumber", verificationData.getString("voucherNumber"));
                d.put("transId", verificationData.getString("transId"));
                SqlDispatch sqlDispatch = new SqlDispatch(myConfig);
                sqlDispatch.insert("verification", d);
            }
            //核销 与 核销撤销都返回成功的
            returnJson.setApiStatus(1);
            returnJson.setResult(mark + "成功");
            return returnJson.toString();
        }


        returnJson.setApiStatus(1);
        returnJson.setResult(mark + "失败：" + httpResponse.getResponseResult());
        return returnJson.toString();
    }
}
